package com.aw.guiguzi.module.manager._user.adapter

import android.widget.LinearLayout
import com.aw.baselib.util.AwDataUtil
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.UserInfoBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

class UserListAdapter :
    BaseQuickAdapter<UserInfoBean, BaseViewHolder>(R.layout.adapter_manager_user_list) {
    private var mList: MutableList<UserInfoBean>? = ArrayList()

    init {
        if (mList == null) {
            mList = ArrayList()
        }
    }

    override fun convert(helper: BaseViewHolder, bean: UserInfoBean) {
        helper.setText(R.id.tv_username, bean.username)
            .setText(R.id.tv_nickname, bean.nickname)
            .setText(
                R.id.tv_lastLoginTime,
                if (AwDataUtil.isEmpty(bean.formatLoginTime())) "--" else bean.formatLoginTime()
            )
        val ll_layout = helper.getView<LinearLayout>(R.id.ll_layout)
        if (helper.layoutPosition % 2 == 0) {
            ll_layout.setBackgroundColor(mContext.resources.getColor(com.aw.baselib.R.color.color_item_1))
        } else {
            ll_layout.setBackgroundColor(mContext.resources.getColor(com.aw.baselib.R.color.color_item_2))
        }
    }

    fun addAllData(dataList: MutableList<UserInfoBean>?) {
        mList = dataList
        setNewData(mList)
        notifyDataSetChanged()
    }

    fun clearData() {
        if (mList != null) {
            val startPosition = 0 //hasHeader is 1
            val preSize = mList!!.size
            if (preSize > 0) {
                mList!!.clear()
                notifyItemRangeRemoved(startPosition, preSize)
            }
        }
    }
}